package com.hebeu.gfh.controller;

import com.hebeu.gfh.pojo.po.ClubApplication;
import com.hebeu.gfh.pojo.vo.IdAndName;
import com.hebeu.gfh.pojo.vo.appinfo;
import com.hebeu.gfh.pojo.vo.submitedApplication;
import com.hebeu.gfh.pojo.vo.appli;
import com.hebeu.gfh.service.clubApplicationService;
import com.hebeu.gfh.service.clubsService;
import com.hebeu.gfh.service.userService;
import com.hebeu.gfh.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 刘剑利
 * @Version: v1.0
 */
@RestController
@RequestMapping("/clubApplication")
public class clubApplicationController {
    @Autowired
    clubApplicationService applicationService;
    @Autowired
    clubsService clubsService;
    @Autowired
    userService userService;
    //提交申请
    @PostMapping("/submitApplication")
    public R submitApplication(ClubApplication clubApplication){
        clubApplication.setApplicationState(2);
        try {
            applicationService.submitApplication(clubApplication);
        }catch (Exception e){
            System.out.println("异常为"+e);

            return R.error(500,"申请失败");
        }
        return R.ok();
    }
    //获取已经提交的请求
    @PostMapping("/findsubmitdApplication")
    public R findsubmitdApplication(int userId){
        List<submitedApplication> submitedApplicationList;
        //先根据userid获得submitapplication，在根据所获取的记录中的club_id去获取clubInfo
        submitedApplicationList = applicationService.finfsubmitdApplication(userId);
        return R.ok(submitedApplicationList);
    }

    //根据userId获取已提交的申请
    @PostMapping("/getApplicationByUserId")
    public R getApplicationByUserId(IdAndName idAndName){
        List<ClubApplication> clubApplicationList =clubsService.getApplicationByUserId(idAndName);
        List<appli>  application1s  =new ArrayList<>();
        for(ClubApplication application:clubApplicationList){
            String clubName = clubsService.getClubNameById(application.getClubId());
            appinfo u = userService.findUserByuserId(application.getUserId());
            String username = u.getUserInfo().getUsername();
         appli a =new appli();
         a.setUsername(username);
         a.setClubName(clubName);
         a.setClubApplication(application);
         application1s.add(a);
        }
        return R.ok(application1s);
    }

    //通过
    @PostMapping("/passApplication")
    public R passApplication(int applicationId){
        try {
            applicationService.passApplication(applicationId);
        }catch (Exception e){
            return R.error(500,"操作失败");
        }
        return R.ok("操作成功");
    }

    //驳回
    @PostMapping("/unpassApplication")
    public R unpassApplication(int applicationId){
        try {
            applicationService.unpassApplication(applicationId);
        }catch (Exception e){
            return R.error(500,"操作失败");
        }
        return R.ok("操作成功");
    }

    //根据applicationId获取信息
    @PostMapping("/getApplicationById")
    public R getApplicationById(int applicationId){
        ClubApplication application = applicationService.getApplicationById(applicationId);
        return R.ok(application);
    }
}
